package com.ruoyi.project.newhope.warningLog.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.newhope.device.domain.NewHopeInfraredDevice;
import com.ruoyi.project.newhope.device.mapper.NewHopeInfraredDeviceMapper;
import com.ruoyi.project.newhope.inspection.domain.NewHopeInspection;
import com.ruoyi.project.newhope.inspection.mapper.NewHopeInspectionMapper;
import com.ruoyi.project.newhope.material.domain.NewHopeMaterial;
import com.ruoyi.project.newhope.material.mapper.NewHopeMaterialMapper;
import com.ruoyi.project.newhope.materialInspection.domain.NewHopeMaterialInspection;
import com.ruoyi.project.newhope.materialInspection.mapper.NewHopeMaterialInspectionMapper;
import com.ruoyi.project.newhope.warningLog.domain.NewHopeWarningLog;
import com.ruoyi.project.newhope.warningLog.mapper.NewHopeWarningLogMapper;
import com.ruoyi.project.newhope.warningLog.service.INewHopeWarningLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 指标报警历史Service业务层处理
 *
 * @author ruoyi
 * @date 2021-02-08
 */
@Service
public class NewHopeWarningLogServiceImpl implements INewHopeWarningLogService {
    @Autowired
    private NewHopeWarningLogMapper newHopeWarningLogMapper;
    @Autowired
    private NewHopeInfraredDeviceMapper newHopeInfraredDeviceMapper;
    @Autowired
    private NewHopeMaterialMapper newHopeMaterialMapper;
    @Autowired
    private NewHopeMaterialInspectionMapper newHopeMaterialInspectionMapper;
    @Autowired
    private NewHopeInspectionMapper newHopeInspectionMapper;

    /**
     * 查询指标报警历史
     *
     * @param id 指标报警历史ID
     * @return 指标报警历史
     */
    @Override
    public NewHopeWarningLog selectNewHopeWarningLogById(Long id) {
        NewHopeWarningLog newHopeWarningLog = newHopeWarningLogMapper.selectNewHopeWarningLogById(id);
        if (newHopeWarningLog.getInfraredDeviceId() != null) {
            NewHopeInfraredDevice newHopeInfraredDevice = newHopeInfraredDeviceMapper.selectNewHopeInfraredDeviceById(newHopeWarningLog.getInfraredDeviceId());
            if (newHopeInfraredDevice != null) {
                newHopeWarningLog.setInfraredDeviceLocation(newHopeInfraredDevice.getLocation());
                newHopeWarningLog.setInfraredDeviceName(newHopeInfraredDevice.getDesc());
            }
        }
        if (newHopeWarningLog.getMaterialId() != null) {
            NewHopeMaterial material = newHopeMaterialMapper.selectNewHopeMaterialById(newHopeWarningLog.getMaterialId());
            if (material != null) {
                newHopeWarningLog.setMaterialCode(material.getMaterialCode());
                newHopeWarningLog.setMaterialName(material.getMaterialDesc());
            }
            NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(newHopeWarningLog.getInspectionId());
            if (inspection != null) {
                newHopeWarningLog.setInspectionCode(inspection.getInspectionCode());
                newHopeWarningLog.setInspectionName(inspection.getInspectionDesc());
            }
            if (newHopeWarningLog.getInspectionId() != null && newHopeWarningLog.getMaterialId() != null) {
                NewHopeMaterialInspection materialInspection = newHopeMaterialInspectionMapper.selectNewHopeMaterialInspectionByMaterialCodeAndInspectionId(newHopeWarningLog.getMaterialId(), newHopeWarningLog.getInspectionId());
                if (materialInspection != null) {
                    newHopeWarningLog.setMaxValue(materialInspection.getMax());
                    newHopeWarningLog.setMinValue(materialInspection.getMin());
                }
            }
        }
        return newHopeWarningLog;
    }

    /**
     * 查询指标报警历史列表
     *
     * @param newHopeWarning 指标报警历史
     * @return 指标报警历史
     */
    @Override
    public List<NewHopeWarningLog> selectNewHopeWarningLogList(NewHopeWarningLog newHopeWarning) {
        List<NewHopeWarningLog> newHopeWarningLogs = newHopeWarningLogMapper.selectNewHopeWarningLogList(newHopeWarning);
        for (NewHopeWarningLog newHopeWarningLog : newHopeWarningLogs) {
            if (newHopeWarningLog.getInfraredDeviceId() != null) {
                NewHopeInfraredDevice newHopeInfraredDevice = newHopeInfraredDeviceMapper.selectNewHopeInfraredDeviceById(newHopeWarningLog.getInfraredDeviceId());
                if (newHopeInfraredDevice != null) {
                    newHopeWarningLog.setInfraredDeviceLocation(newHopeInfraredDevice.getLocation());
                    newHopeWarningLog.setInfraredDeviceName(newHopeInfraredDevice.getDesc());
                }
            }
            if (newHopeWarningLog.getMaterialId() != null) {
                NewHopeMaterial material = newHopeMaterialMapper.selectNewHopeMaterialById(newHopeWarningLog.getMaterialId());
                if (material != null) {
                    newHopeWarningLog.setMaterialCode(material.getMaterialCode());
                    newHopeWarningLog.setMaterialName(material.getMaterialDesc());
                }
                NewHopeInspection inspection = newHopeInspectionMapper.selectNewHopeInspectionById(newHopeWarningLog.getInspectionId());
                if (inspection != null) {
                    newHopeWarningLog.setInspectionCode(inspection.getInspectionCode());
                    newHopeWarningLog.setInspectionName(inspection.getInspectionDesc());
                }
                if (newHopeWarningLog.getInspectionId() != null && newHopeWarningLog.getMaterialId() != null) {
                    NewHopeMaterialInspection materialInspection = newHopeMaterialInspectionMapper.selectNewHopeMaterialInspectionByMaterialCodeAndInspectionId(newHopeWarningLog.getMaterialId(), newHopeWarningLog.getInspectionId());
                    if (materialInspection != null) {
                        newHopeWarningLog.setMaxValue(materialInspection.getMax());
                        newHopeWarningLog.setMinValue(materialInspection.getMin());
                    }
                }
            }
        }
        return newHopeWarningLogs;
    }

    /**
     * 新增指标报警历史
     *
     * @param newHopeWarningLog 指标报警历史
     * @return 结果
     */
    @Override
    public int insertNewHopeWarningLog(NewHopeWarningLog newHopeWarningLog) {
        newHopeWarningLog.setCreateTime(DateUtils.getNowDate());
        return newHopeWarningLogMapper.insertNewHopeWarningLog(newHopeWarningLog);
    }

    /**
     * 修改指标报警历史
     *
     * @param newHopeWarningLog 指标报警历史
     * @return 结果
     */
    @Override
    public int updateNewHopeWarningLog(NewHopeWarningLog newHopeWarningLog) {
        newHopeWarningLog.setUpdateTime(DateUtils.getNowDate());
        return newHopeWarningLogMapper.updateNewHopeWarningLog(newHopeWarningLog);
    }

    /**
     * 删除指标报警历史对象
     *
     * @param id 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeWarningLogByIds(String id) {
        return newHopeWarningLogMapper.deleteNewHopeWarningLogByIds(Convert.toStrArray(id));
    }

    /**
     * 删除指标报警历史信息
     *
     * @param id 指标报警历史ID
     * @return 结果
     */
    @Override
    public int deleteNewHopeWarningLogById(Long id) {
        return newHopeWarningLogMapper.deleteNewHopeWarningLogById(id);
    }

}
